/* ---------------------------------------------------------------------------
   This is a part of the ActivCard Samples. 
   Copyright (C) 2004 ActivCard Corp.
   All rights reserved.

   This source code is only intended as a supplement to 
   ActivCard Development Tools and/or documentation.
   See these sources for detailed information regarding the 
   ActivCard samples programs.

   Disclaimer:
   ActivCard makes no representations or warranties with respect to the
   contents or use of this software, and specifically disclaims any express
   or implied warranties of merchantability or fitness for any particular
   purpose. Further, ActivCard reserves the right to revise this software
   and to make changes to its content, at any time, without obligation to
   notify any person or entity of such revisions or changes.
   Further, ActivCard makes no representations or warranties with 
   respect to any software, and specifically disclaims any express or 
   implied warranties of merchantability or fitness for any particular purpose.
--------------------------------------------------------------------------- */



/*************/
/** INCLUDE **/
/*************/
#include <stdio.h>
#include "sdbapi.h"
#include "splutil.h"


/*---------------------------------------------------------------------------
 *
 * Function:	GetString
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
void GetString(
		const char *text,
		char *buffer,
		int buffer_size)
{
	int	i;

	printf("\n\t%s : ", text);
	memset(buffer, 0, buffer_size);
	fgets(buffer, buffer_size, stdin);
	for (i = (int)strlen(buffer) - 1; i >= 0; i--)
	{
		if (buffer[i] == '\n')
		{
			buffer[i] = '\0';
			break;
		}
	}
	fflush(stdin);
}


/*---------------------------------------------------------------------------
 *
 * Function:	LoadBuffer
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
int LoadBuffer(
		char *file,
		char **buf)
{
	FILE *fp = NULL;
	int len,eof;
	char *ptr;
	int err = 0;
	char file_path[MAX_PATH];

	if (!buf || !(file && file[0])) {
		err = -1;
		goto Errors;
	}

	*buf = NULL;

	if (( fp = fopen( file, "r" )) == NULL ) 
	{
		sprintf(file_path,"..\\%s",file);
		if (( fp = fopen( file_path, "r" )) == NULL ) 
		{
			err = -1;
			goto Errors;
		}
	}

	if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
		err = -1;
		goto Errors;
	}

	len = ftell( fp )+1;

	if (( *buf = (char *)malloc( len )) == NULL ) {
		err = -1;
		goto Errors;
	}
	memset(*buf,0,len);
	if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
		err = -1;
		goto Errors;
	}

	ptr = *buf;
	while (!feof( fp )){
		fread( ptr, 1, 1, fp );
		ptr++;
	}
	eof = feof( fp );
    *ptr = '\0';

Errors:
    if (fp)
	    fclose( fp );
	return(err);
}


/*---------------------------------------------------------------------------
 *
 * Function:	WriteBuffer
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
int WriteBuffer(
		char *buf, 
		int buflen, 
		char *file)
{

	FILE *fp;
	int err = 0;

	if (!buf || buflen <0 || !(file && file[0])) {
		err = -1;
		goto Errors;
	}

	if (( fp = fopen( file, "w" )) == NULL ) {
		fprintf(stderr,"Error opening file : %s", file);
		err = -1;
		goto Errors;
	}

	fwrite(buf,buflen,1,fp);

	fclose( fp );

Errors:
	return(err);

}

/*---------------------------------------------------------------------------
 *
 * Function:	LoadSDB
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
int LoadSDB(
		SDB *pSDB, 
		char *filename)
{
	FILE	*f = NULL;
	long	lSize;
	int	res;
	unsigned char *pTmp;

	if (*filename)
		f = fopen(filename, "rb");
	if (! f)
		return -1;
	/* Get file len */
	fseek(f, 0, SEEK_END);
	lSize = ftell(f);
	fseek(f, 0, SEEK_SET);
	pTmp = malloc(lSize);
	if (!pTmp) {
		printf("Memory allocation error\n");
		return -1;
	}
    memset(pTmp, 0, lSize);

	fread(pTmp, lSize, 1, f);
	fclose(f);
	
	res = SDBNew(pSDB, pTmp, lSize);
	free(pTmp);
	if (res != SDB_ERR_NOERROR) {
		return -1;
	}
	return 0;
}


/*---------------------------------------------------------------------------
 *
 * Function:	DumpSDB
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
void DumpSDB(
		SDB sdb, 
		int sdblen)
{
	int	i, j=0;

	printf("\nSDB dump :\n");

	for (i = 0; i < sdblen; i++)
	{
		if (i && !(i%8))
		{
			printf("\t");
			for(j = i-8; j < i;j++)
			{
				if(isprint(sdb[j]))
				{
					printf("%c",sdb[j]);
				} 
				else 
				{
					printf(".");
				}
			}
			printf("\n");
		}
		if (!(i%8))
			printf("%04x: ", i);
		printf(" %02x",sdb[i]);
	}
	
	{
	int k = i;
	while(k%8) 
	{
		printf("   ");
		k++;
	}
	printf("\t");
	for(; j < i;j++)
	{
		if(isprint(sdb[j])) 
		{
			printf("%c",sdb[j]);
		} 
		else 
		{
			printf(".");
		}
	}
	}
	printf("\n");
}


/*---------------------------------------------------------------------------
 *
 * Function:	SaveSDB
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
int SaveSDB(SDB *pSDB, char *pszFilename, const char *pszKey)
{
	FILE *pFile = NULL;
	int	nSize;
	int	nRet = 0;
	
	if (pszFilename == NULL)
	{
		fprintf(stderr,"bad file name\n");
		nRet = -1;
		goto EndLabel;
	}
	pFile = fopen(pszFilename, "w+b");

	if (pFile == NULL)
	{
		perror("fopen failed");
		nRet = -1;
		goto EndLabel;
	}

	nRet = SDBGetLen(pszKey, pSDB, &nSize);
	if (nRet != SDB_ERR_NOERROR)
	{
		printf("Error : %d\n", nRet);
		fclose(pFile);
		nRet = -1;
		goto EndLabel;
	}

	if (nSize != (int) fwrite(*pSDB, sizeof(char), (unsigned long)nSize, pFile))	
	{
		perror("fwrite failed");
		nRet = -1;
		goto EndLabel;
	}

EndLabel:
	if(pFile != NULL)
	{
		fclose(pFile);
	}

	return nRet;
}
/*---------------------------------------------------------------------------
 *
 * Function:	OverwriteSDB
 *
 * Comments:	None
 *
 *---------------------------------------------------------------------------*/
int OverwriteSDB(
		const char *pszKey, 
		PSDB pSrcSDB, 
		PSDB pDestSDB)
{
    int nRet = 0;

    int nSrcLength, nDestlength; 
    if (!pSrcSDB || !pDestSDB || !*pSrcSDB || !*pDestSDB)
		return (nRet);

    nRet = SDBGetLen(pszKey, pSrcSDB, &nSrcLength);
    if(nRet)
		return (nRet); 
    nRet = SDBGetLen(pszKey, pDestSDB, &nDestlength);
    if(nRet)
		return (nRet); 

    /* Just in case not to get into trouble */
    if (nSrcLength != nDestlength)
		return(nRet);

    /* Everything is ok copy input in output */
    memcpy(*pDestSDB, *pSrcSDB, nSrcLength);

    /* And free source */
    SDBFree(pSrcSDB);

    return nRet;
}

